import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { environment } from '../../../environments/environment';
import { Observable } from 'rxjs';
import { BaseResponseModel } from '../../@common/models/base-response.model';

@Injectable()
export class CaptchaService {
  private readonly apiUrl;

  constructor(private readonly httpClient: HttpClient) {
    this.apiUrl = environment.apiUrl + 'captcha';
  }

  /**
   * generate captcha
   * @param email
   */
  generateCaptcha = (email: string): Observable<BaseResponseModel> => {
    return this.httpClient.get<BaseResponseModel>(`${this.apiUrl}/${email}`);
  };

  /**
   * check user's captcha
   * @param email
   * @param captcha
   */
  checkCaptcha = (
    email: string,
    captcha: string
  ): Observable<BaseResponseModel> => {
    return this.httpClient.post<BaseResponseModel>(this.apiUrl, {
      email,
      captcha
    });
  };
}
